package graph_viewer.animator;

import java.awt.*;
import javax.swing.*;

import graph_viewer.canvas.*;
import graph_viewer.structure.*;
import graph_viewer.util.*;
/**
 * the super interface of Animator.
 * now no public method defined.
 */

public class Size_Animator extends Basic_Animator{
	
	public Size_Animator(){
	//	Debug.println( "Size_Animator Created!", 3 );
	}
	
	public boolean do_animation( Animator_Metrics a_m, Drawer_Canvas graph_canvas ){
		//Debug.println( "Size_Animator.do_animation on graph_canvas!", 3 );

		Node_Position	
			from_position	= a_m.get_from_position(),
			to_position		= a_m.get_to_position();

		double
			time_interval	= a_m.get_time_interval();

		Point[]	
			from_position_corners	= from_position.find_corners(),
			to_position_corners		= to_position.find_corners();		
		
		boolean is_moved	 = false;

		for ( int i = 0; i < from_position_corners.length; i++ ){
			//Debug.println( "From " + from_position_corners[i] + " to " + to_position_corners[i], 3 );
			if ( move_along_line( from_position_corners[i], to_position_corners[i], set_up_motion_distance( from_position_corners[i], to_position_corners[i] ) ) ){
				is_moved = true;				
				//Debug.println( "Moved corners: " + from_position_corners[i] + " to " + to_position_corners[i], 3 );
			}
			
		}
				
		if ( is_moved ){
			JComponent animation_board	= graph_canvas.get_animation_board();
			clear_range( graph_canvas );
			
			resize_by_corner_points( from_position, from_position_corners );

			Node moving_node	= graph_canvas.get_graph().search( a_m.get_node_label() );
			moving_node.set_position( from_position );
			
			Node_Position_Helper.set_cluster_node_position( moving_node, graph_canvas.get_graph() );

			Animator_Util.pause( SIZE_ANIMATION_TIME_INTERVAL );			
		}

		return is_moved;
	}

	//template hooker implementation//===================================================
	protected double find_min_move_distance(){
		return SIZE_ANIMATION_MOVE_DISTANCE;
	}

	protected double find_animation_rate(){
		return SIZE_ANIMATION_RATE;
	}
	//============================================================================		
	
	private void resize_by_corner_points( Node_Position np, Point[] corners ){
		Point
			left_top_corner		= corners[0],
			right_top_corner		= corners[1],
			right_bottom_corner	= corners[2],
			left_bottom_corner	= corners[3];

		double 
			width			= right_top_corner.getX() - left_top_corner.getX(),
			height		= left_bottom_corner.getY() - left_top_corner.getY();
			//x_center		= left_top_corner.getX() + width/2,
			//y_center		= left_top_corner.getY() + height/2;
		
		np.set_width( (int)width );
		np.set_height( (int)height );
		//np.set_x_center( (int)x_center );
		//np.set_y_center( (int)y_center );

	}
		
}//:~